package cn.jbolt.admin.dictionary;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.cache.JBoltDictionaryCache;
import cn.jbolt.core.cache.JBoltDictionaryTypeCache;
import cn.jbolt.core.model.Dictionary;
import cn.jbolt.core.model.DictionaryType;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltDictionaryService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

@UnCheckIfSystemAdmin
@OnlySaasPlatform
@CheckPermission(PermissionKey.DICTIONARY)
@Path("/api/admin/dictionary")
public class DictionaryAdminController extends JBoltApiBaseController {
    @Inject
    JBoltDictionaryService service;

    /**
     * 单个详情信息获取
     */
    @JBoltHttpGet
    public void index(){
        renderJBoltApiRet(findModelById());
    }

    public void checkAndInit() {
        renderJBoltApiRet(service.checkAndInit());
    }

    /**
     * 数据接口条件查询
     * @param typeId
     * @param enable
     * @param keywords
     */
    @JBoltHttpGet
    public void datas(@Para(TYPE_ID_PARAM_NAME)Long typeId,
                      @Para(value = ENABLE_PARAM_NAME,defaultValue = "true")Boolean enable,
                      @Para(KEYWORDS_PARAM_NAME)String keywords){
        if(notOk(typeId)){
            renderJBoltApiSuccess();
            return;
        }
        DictionaryType type = JBoltDictionaryTypeCache.me.get(typeId);
        if(type == null) {
            renderJBoltApiSuccess();
            return;
        }
        List<Dictionary> dics = service.getAdminListByTypeId(typeId,keywords, enable);
        Kv extraData=Kv.by("type", type);
        //使用这个专门的方法 render出去
        renderJBoltApiSuccessWithData(dics,extraData);
    }

    /**
     * select radio checkbox可用的Option 列表接口
     * @param typeKey 可选 二选一
     * @param typeId  可选 二选一
     */
    @JBoltHttpGet
    @UnCheck
    public void options(@Para("typeKey")String typeKey,@Para("typeId")Long typeId){
        if(isOk(typeKey)){
            renderJBoltApiSuccessWithData(JBoltDictionaryCache.me.getEnableOptionsByTypeKey(typeKey));
            return;
        }
        if(isOk(typeId)){
            renderJBoltApiSuccessWithData(JBoltDictionaryCache.me.getEnableOptionsByTypeId(typeId));
            return;
        }
        renderJBoltApiFail(JBoltMsg.PARAM_ERROR+":typeKey or typeId 二选一");
    }

    /**
     * apiDatas 列表接口
     * @param typeKey 可选 二选一
     * @param typeId  可选 二选一
     */
    @JBoltHttpGet
    @UnCheck
    public void apiDatas(@Para("typeKey")String typeKey,@Para("typeId")Long typeId){
        if(isOk(typeKey)){
            renderJBoltApiSuccessWithData(JBoltDictionaryCache.me.getEnableApiDatasByTypeKey(typeKey));
            return;
        }
        if(isOk(typeId)){
            renderJBoltApiSuccessWithData(JBoltDictionaryCache.me.getEnableApiDatasByTypeId(typeId));
            return;
        }
        renderJBoltApiFail(JBoltMsg.PARAM_ERROR+":typeKey or typeId 二选一");
    }

    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("")Dictionary dictionary){
        renderJBoltApiRet(service.save(dictionary));
    }
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")Dictionary dictionary){
        renderJBoltApiRet(service.update(dictionary));
    }
    /**
     * 上移
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void up(){
        renderJBoltApiRet(service.doUp(getId()));
    }
    /**
     * 下移
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void down(){
        renderJBoltApiRet(service.down(getId()));
    }
    /**
     * 初始化顺序
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void initRank(){
        renderJBoltApiRet(service.initRank(getTypeId()));
    }
    /**
     * 清空分类下的字典数据
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void clearByType(){
        renderJBoltApiRet(service.clearByTypeId(getTypeId()));
    }

    /**
     * 切换enable属性值
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable(){
        renderJBoltApiRet(service.toggleEnable(getId()));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete(){
        renderJBoltApiRet(service.deleteDictionaryById(getId(),false));
    }

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 改变顺序
     * @param id
     * @param oriPid
     * @param newPid
     * @param oriRank
     * @param newRank
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void changeSortRank(Long id, Long oriPid, Long newPid, Integer oriRank, Integer newRank) {
        renderJBoltApiRet(service.changeSortRank(id, oriPid, newPid, oriRank, newRank));
    }

}
